/*
 * TCSS 360 - Winter 2014
 * TESK Software
 */
package view.Papers;


import SQL.DB;
import javax.swing.JPanel;

import view.MainFrame;
import view.userProfile.ReviewerView;

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.Font;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.border.TitledBorder;
import javax.swing.border.BevelBorder;
import java.awt.GridLayout;
import javax.swing.ListSelectionModel;

/**
 * Allows a reviewer to submit a review of a paper.
 * 
 * @author Kon, Thang, Emily
 * @version March 20, 2014
>>>>>>> .r197
 */
@SuppressWarnings("serial")
public class SubmitReviewView extends JPanel{
	
	/** The frame that the panel is associated with. */
	private MainFrame my_frame;

	/** A JList containing the papers available to the reviewer. */
	private JList paperlist;
	
	/** The file to be uploaded. */
	private File uploadFile;
	
	/** The conference the reviewer is currently using. */
	private String current_conference;

	/**
	 * Constructs the panel to submit a review. 
	 * 
	 * @param the_frame the frame to associate the panel with
	 * @param the_conference the conference the reviewer is currently working with
	 */
	public SubmitReviewView(MainFrame the_frame, String the_conference) {
		setBackground(Color.WHITE);
		my_frame = the_frame;
		current_conference = the_conference;
		setLayout(new BorderLayout());

		JPanel northPanel = new JPanel();
		northPanel.setBorder(new TitledBorder(null, "Reviewer", TitledBorder.LEFT, TitledBorder.TOP, null, Color.BLUE));
		northPanel.setBackground(Color.GREEN);
		add(northPanel, BorderLayout.NORTH);

		JLabel lblNewLabel = new JLabel("Submit a Review");
		lblNewLabel.setFont(new Font("Urdu Typesetting", Font.PLAIN, 18));
		northPanel.add(lblNewLabel);

		JPanel centerPanel = new JPanel();
		centerPanel.setBackground(Color.WHITE);
		add(centerPanel, BorderLayout.CENTER);
		centerPanel.setLayout(new GridLayout(0, 2, 0, 0));

		//getList of papers for paperlist
		ArrayList<String> scPaperList = my_frame.db.getPapersAssignedForReviewer(my_frame.currentUser[0][0]);
		String[] paperArray = new String[scPaperList.size()];
		int j = 0;
		for (int i = 0; i < scPaperList.size(); i++) {
			if (scPaperList.get(i) != null) {
				paperArray[j] = scPaperList.get(i);
				j++;
			}
		}

		JScrollPane scrollPane = new JScrollPane();
		centerPanel.add(scrollPane); //add this list to Center Panel.


		//add array to paperlist
		paperlist = new JList(paperArray);
		paperlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		paperlist.setFont(new Font("Sylfaen", Font.PLAIN, 14));
		paperlist.setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null), "Titles", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		scrollPane.setViewportView(paperlist);

		//picture label
		
		ImageIcon icon = new ImageIcon(getClass().getResource("/gin.jpg"));
		final JLabel pic = new JLabel();
		//pic.setIcon(new ImageIcon("..\\Conference System\\src\\gin.jpg"));
		pic.setIcon(icon);
		JPanel centerRightPanel = new JPanel();
		centerRightPanel.setBackground(Color.WHITE);
		centerPanel.add(centerRightPanel);
		centerRightPanel.add(pic);
		centerRightPanel.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel uploadPanel = new JPanel();
		uploadPanel.setBackground(Color.WHITE);
		centerRightPanel.add(uploadPanel);
		uploadPanel.setLayout(null);

		JButton btnUpload = new JButton("Upload Review");
		btnUpload.setForeground(Color.BLUE);
		btnUpload.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnUpload.setBounds(37, 56, 176, 35);
		uploadPanel.add(btnUpload);
		btnUpload.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				//if the user didnt select anything or the value at what user selected is null or nothing.
				//then pop up a warning.
				if (paperlist.isSelectionEmpty() || paperlist.getSelectedValue() == null){
					JOptionPane.showMessageDialog(my_frame, "No paper is selected", "Error!", JOptionPane.ERROR_MESSAGE);
				}
				else {
					JFileChooser fc = new JFileChooser();
					fc.showOpenDialog(fc);
					try {

						uploadFile = fc.getSelectedFile();
						JLabel fileNameLabel = new JLabel("");
						fileNameLabel.setText(uploadFile.getName());
					}
					catch(NullPointerException the_e){

					}
				}
			}
		});

		JPanel southPanel = new JPanel();
		southPanel.setBackground(Color.GREEN);
		add(southPanel, BorderLayout.SOUTH);

		JButton submitButton = new JButton("Submit");
		submitButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//disable, if no title selected, give dialog error.//kon
				//CopyDocuments.copy(file,);
				if (paperlist.isSelectionEmpty() 
						|| paperlist.getSelectedValue() == null 
						|| uploadFile == null){
					JOptionPane.showMessageDialog(my_frame, "No paper is selected", "Error!", JOptionPane.ERROR_MESSAGE);
				}
				else {
					try {
						CopyDocuments.copy(uploadFile, new File("Reviews/" + uploadFile.getName()));

						String paperName = (String)paperlist.getSelectedValue();
						String author = my_frame.db.getAuthor(paperName, current_conference);
						String reviewName = uploadFile.getName();
						my_frame.db.addReview(author, current_conference,  reviewName, paperName);

						JOptionPane.showMessageDialog(my_frame, "Sucessfully Submitted Review!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
					} catch (IOException e1) {
						System.out.print("Copy Failed");
						e1.printStackTrace();
					}
				}
			}
		});
		southPanel.add(submitButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_frame.setNewPanel(new ReviewerView(my_frame));
			}
		});
		southPanel.add(cancelButton);
	}
}
